<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getConfig">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="key" label="键" width="180" sortable>
      </el-table-column>
      <el-table-column prop="value" label="值" width="160" sortable>
      </el-table-column>
      <el-table-column prop="intro" label="简介" width="300" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="total, prev, pager, next"
          @current-change="handleCurrentChange"
          :page-size="query.pageSize"
          :total="pageInfo.total"
          :current-page="query.currentPage"
          style="float:right;">
      </el-pagination>
    </el-col>

  </section>
</template>

<script>
import {batchRemoveUser, removeUser} from "../../api/api";

export default {
  data() {
    return {
      filters: {
        name: ''
      },
      listLoading: false,
      pageInfo:{
        total: 0,
        rows: []
      },
      query: {
        currentPage: 1,
        pageSize: 10,
        keyword: ''
      },

      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      //编辑界面数据
      editForm: {
        id: null,
        key: '',
        value: '',
        type: null,
        intro: '',
      },
    }
  },
  methods: {
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getConfig();
    },
    getConfig() {
      this.$http.post("/config",this.query)
        .then(result => {
          result = result.data;
          if (result.success) {// true 后端返回了数据
            this.pageInfo = result.data;
          } else {// 后端处理失败
            this.$message({message: result.message,type: 'error'});
          }
        })
        .catch(result => {
          this.$message({message: "网络错误："+result,type: 'error'});
        })
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //删除
    handleDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {type: 'warning'})
        .then(() => {
          this.listLoading = true;
        //NProgress.start();
        let para = { id: row.id };
        removeUser(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({message: '删除成功', type: 'success'});
          this.getUsers();
        });
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit(index, row) {
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd() {
      this.editFormVisible = true;
      this.editForm = {
        id: null,
        key: '',
        value: '',
        type: null,
        intro: ''
      };
    },
    //批量删除
    batchRemove() {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {type: 'warning'})
        .then(() => {
          this.listLoading = true;
          let para = { ids: ids };
          batchRemoveUser(para).then((res) => {
            this.listLoading = false;
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.getUsers();
          });
        })
        .catch(() => {

      });
    }
  },
  mounted() {
    this.getConfig();
  }
}

</script>

<style scoped>

</style>